<?php

class AttributeController extends BaseController
{
	
	/**
	 * 新建属性
	 */
	public function actionCreate()
	{
		$model=new Attribute;

		if(isset($_POST['Attribute']))
		{
			$atts = $_POST['Attribute'];
			//查询当前有多少个规格
			if($atts['za_type']==2){
				$count = count($model->FindAll('zca_id='.$atts['zca_id'].' AND za_type=2'));
			}
			//判断规格是否超过限制
			if( $count >= 2){
				//错误
				$this->render('../index/error',array('message'=>t('Spe Max','adm')
						));
				exit;
			}
			//查询排序最大的数字
			$maxlist = $model->Find(array(
										    'select'=>'za_listorder',
										    'condition'=>'zca_id='.$atts['zca_id'],
										    'order'=>'za_listorder DESC',
										    )
			);
			//print_r($maxlist->za_listorder);exit;				
			$model->attributes=$_POST['Attribute'];
			$model->za_value = loseSpace($atts['za_value']);//过滤空格
			$model->za_listorder = $maxlist->za_listorder + 1; //最大的排序值加1
			if($model->save())
				$this->redirect(array('admin','id'=>$model->zca_id));
		}
	}

	/**
	 *更新属性设置
	 */
	public function actionUpdate()
	{
		$data = $_POST['attribute'];
		$num = count($data['za_text_type']);
		for($i=0;$i<$num;$i++){
			//if(false==$data['za_id'][$i]){
				$model = Attribute::model()->findByPk($data['za_id'][$i]);
				$model->za_title		= $data['za_title'][$i];
				$model->za_value		= loseSpace($data['za_value'][$i]);
				$model->za_search		= $data['za_search'][$data['za_id'][$i]];
				$model->za_photo		= $data['za_photo'][$data['za_id'][$i]];
				$model->za_text_type	= $data['za_text_type'][$i];
				$model->za_listorder	= $i;
				$model->save();
			/*}else{
				$model = Attribute::model()->__construct('insert');
				$model->za_title		= $data['za_title'][$i];
				$model->zca_id		= $data['zca_id'][$i];
				$model->za_value		= $data['za_value'][$i];
				$model->za_search		= $data['za_search'][$data['za_id'][$i]];
				$model->za_photo		= $data['za_photo'][$data['za_id'][$i]];
				$model->za_text_type	= $data['za_text_type'][$i];
				$model->za_listorder	= $i;
				Attribute::model()->__construct(array('za_title','zca_id','za_value','za_search','za_photo','za_text_type','za_listorder'));
			}*/
		}
		$this->redirect(array('admin','id'=>$model->zca_id));
	}

	/**
	 *删除属性规格
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$model = $this->loadModel($id);
		$categoryid = $model->zca_id;
		$model->delete();
		$this->redirect(array($this->pre_module_url.'/attribute/admin','id'=>$categoryid));
	}


	/**
	 * 选择行业
	 */
	public function actionSelectCategory()
	{
		if(isset($_GET['next'])){
			//没有选择一级分类，或没有选择二级分类时，跳转到错误页面。
			if(!isset($_GET['cateid3'])||(int)($_GET['cateid3'])==0){
				if((int)$_GET['cateid2']==0)
					throw new CHttpException(404,'The requested page does not exist.');
			}
			//选择三级分类时，取三级分类，没有选择三级分类时，取二级分类。
			$categoryid = (isset($_GET['cateid3'])&&(int)($_GET['cateid3'])!=0)?(int)($_GET['cateid3']):(int)($_GET['cateid2']);
			$this->redirect(array($this->pre_module_url.'/attribute/admin','id'=>$categoryid));
		}
		if(isset($_GET['zam_title'])){
			$title = trim($_GET['zam_title']);
			$model = new Category();
			$model->unsetAttributes();
			$model->zca_title = $title;
			$model->zca_level = '>1';

			$this->render('selectcategory',array(
					'model'=>$model,
			));
			exit;
		}
		$this->render('selectcategory');
	}
	/**
	 * 分类属性管理.
	 */
	public function actionAdmin()
	{
		if(isset($_GET['id'])){
			$model=new Attribute;
			$category=Category::model()->findByPk((int)$_GET['id']);
			$model->zca_id=$category->zca_id;
			$this->render('admin',array(
				'category'=>$category,
					 'model'=>$model,
			));
		}else{
			throw new CHttpException(404,'Invalid request. Please do not repeat this request again.');
		}
	}
	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Attribute::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='attribute-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
